package com.heisenhub.file.utils;

import com.google.gson.Gson;
import com.heisnenhub.common.module.utils.GsonUtils;
import com.qiniu.common.QiniuException;
import com.qiniu.http.Response;
import com.qiniu.storage.Configuration;
import com.qiniu.storage.Region;
import com.qiniu.storage.UploadManager;
import com.qiniu.storage.model.DefaultPutRet;
import com.qiniu.util.Auth;
import org.springframework.web.bind.annotation.GetMapping;

import java.io.File;
import java.io.IOException;
import java.io.UnsupportedEncodingException;
import java.nio.file.Files;

public class QiniuClientUtils {

    private static final String accessKey = "4HWY0PoaObUJpqbSTacwB9AkjaFezSlaf8G2csOT";
    private static final String secretKey = "WEGG_z1YXQ9Ck4hHZSS-qOedeVCtJNw2ngcKxQWX";
    private static final String bucket = "aike-info";




    public String testGet() throws UnsupportedEncodingException {

        // 获取文件路径时需要写这个桶域名，然后做替换，这个桶域名会被用于加密。所以需要后替换
        String domainOfBucket = "http://aike-info-qos.iot-st4.cmiot-solutions.com";
        String key = "zhy-oss/ape-parking/b6942ec3-c8029d1f/1.png";
        //   String encodedFileName = URLEncoder.encode("zhy-oss/ape-parking/b6942ec3-c8029d1f/2.png", "utf-8").replace("+", "%20");
        String publicUrl = String.format("%s/%s", domainOfBucket, key);

        Auth auth = Auth.create(accessKey, secretKey);
        //1小时，可以自定义链接过期时间
        long expireInSeconds = 3600;
        String finalUrl = auth.privateDownloadUrl(publicUrl, expireInSeconds);
        System.out.println(finalUrl);
        String nginxUrl = finalUrl.replaceFirst("http://aike-info-qos.iot-st4.cmiot-solutions.com",
                "https://qn-oss.onepark.com.cn:8008/aikeInfoQos");
        System.out.println(nginxUrl);
        return "GET";
    }


    public static String   testPost(String imagePath) throws IOException {

        // 图片文件路径
        String path = imagePath; // 替换为你的图片路径

        File imageFile = new File(path);

        byte[] imageBytes = Files.readAllBytes(imageFile.toPath());
        /**
         * 自定义区域域名
         */
        Configuration cfg = new Configuration(new Region.Builder()
                .srcUpHost("https://qn-oss.onepark.com.cn:8008/upQos/")
                .accUpHost("https://qn-oss.onepark.com.cn:8008/upQos/")
                .build());
        cfg.resumableUploadAPIVersion = Configuration.ResumableUploadAPIVersion.V2;
        UploadManager manager = new UploadManager(cfg);

        Auth auth = Auth.create(accessKey, secretKey);
        String upToken = auth.uploadToken(bucket);

        Response put = manager.put(imageBytes, "zhy-oss/ape-parking/b6942ec3-c8029d1f/666", upToken);
        DefaultPutRet putRet = GsonUtils.toBean(put.bodyString(), DefaultPutRet.class);
        System.out.println(putRet.key);
        System.out.println(put.getResponse().code());

        return "POST";
    }





    /**
     * 获取加密图片地址
     * @throws QiniuException
     * @throws UnsupportedEncodingException
     */
    private static void downloadSec(String key){


        String domainOfBucket = "http://aike-info-qos.iot-st4.cmiot-solutions.com";
        String publicUrl = String.format("%s/%s", domainOfBucket, key);

        String accessKey = "4HWY0PoaObUJpqbSTacwB9AkjaFezSlaf8G2csOT";
        String secretKey = "WEGG_z1YXQ9Ck4hHZSS-qOedeVCtJNw2ngcKxQWX";
        Auth auth = Auth.create(accessKey, secretKey);
        //1小时，可以自定义链接过期时间
        long expireInSeconds = 3600;
        String finalUrl = auth.privateDownloadUrl(publicUrl, expireInSeconds);

        String nginxUrl = finalUrl.replaceFirst("http://aike-info-qos.iot-st4.cmiot-solutions.com",
                "https://qn-oss.onepark.com.cn:8008/aikeInfoQos");


    }

}
